﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class LabelControl : UserControl, IControlRulable
    {
        private CommandUIDefinitionClasses.Label innerLabel;

        public CommandUIDefinitionClasses.Label InnerLabel
        {
            get { return innerLabel; }
            set { innerLabel = value; }
        }

        public LabelControl()
        {
            InitializeComponent();
            this.innerLabel = new CommandUIDefinitionClasses.Label();
            this.FillControls();
        }

        public LabelControl(CommandUIDefinitionClasses.Label label)
            : this()
        {
            this.innerLabel = label;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcCommand.Value = this.innerLabel.Command;
                this.strForId.Value = this.innerLabel.ForId;
                this.strId.Value = this.innerLabel.Id;
                this.rcImage16by16.Value = this.innerLabel.Image16by16;
                this.icImage16by16Left.Value = this.innerLabel.Image16by16Left;
                this.icImage16by16Top.Value = this.innerLabel.Image16by16Top;
                this.rcLabelText.Value = this.innerLabel.LabelText;
                this.strqueryCommand.Value = this.innerLabel.QueryCommand;
                this.icSequence.Value = this.innerLabel.Sequence;
                this.strTemplateAlias.Value = this.innerLabel.TemplateAlias;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerLabel.Command = this.rcCommand.Value;
                    this.innerLabel.ForId = this.strForId.Value;
                    this.innerLabel.Id = this.strId.Value;
                    this.innerLabel.Image16by16 = this.rcImage16by16.Value;
                    this.innerLabel.Image16by16Left = this.icImage16by16Left.Value;
                    this.innerLabel.Image16by16Top = this.icImage16by16Top.Value;
                    this.innerLabel.LabelText = this.rcLabelText.Value;
                    this.innerLabel.QueryCommand = this.strqueryCommand.Value;
                    this.innerLabel.Sequence = this.icSequence.Value;
                    this.innerLabel.TemplateAlias = this.strTemplateAlias.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LabelControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerLabel.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
